package com.example.youdao;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private vo_youdao vvv;
    List<vo_youdao> list = new ArrayList<>();
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.button_fanyi)
    Button buttonFanyi;
    @BindView(R.id.text_query)
    TextView textQuery;
    @BindView(R.id.text_translation)
    TextView textTranslation;
    @BindView(R.id.text_phonetic)
    TextView textPhonetic;
    @BindView(R.id.text_explains)
    TextView textExplains;
    @BindView(R.id.list_youdao)
    ListView listYoudao;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Toast.makeText(MainActivity.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
            try {
                JSONObject array = new JSONObject(msg.obj.toString());
                String query = array.getString("query");
                textQuery.setText(query);
                String translation = array.getString("translation");
                textTranslation.setText(translation);
                JSONObject basic = array.getJSONObject("basic");
                String phonet;
                try {
                    phonet = basic.getString("phonetic");
                } catch (JSONException w) {
                    phonet = "";
                }
                textPhonetic.setText(phonet);
                String explains = basic.getString("explains");
                textExplains.setText(explains);
                String errorcode = array.getString("errorCode");
                if (errorcode.equals("0")){

                }else {
                    Toast.makeText(MainActivity.this, "no query", Toast.LENGTH_SHORT).show();
                }
                JSONArray web = array.getJSONArray("web");
                list.clear();
                if (web.length() > 0){
                for (int i = 0; i < web.length(); i++) {
                    JSONObject web1 = (JSONObject) web.get(i);
                    String value = web1.getString("value");
                    String key = web1.getString("key");
                    vvv = new vo_youdao();
                    vvv.setValue(value);
                    vvv.setKey(key);
                    list.add(vvv);
                    listYoudao.setAdapter(new Myadapter());
                }}else {

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button_fanyi)
    public void onViewClicked() {
        getData();
    }

    public void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String keyfrom = "gerenfangyi";
                    String key = "1177293055";
                    String edit = URLEncoder.encode(editText.getText().toString(),"utf-8");
                    URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=" + keyfrom + "&key=" + key + "&type=data&doctype=json&version=1.1&q=" + edit);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    if (conn.getResponseCode() == 200) {
                        InputStream is = conn.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String ss = null;
                        StringBuffer sb = new StringBuffer();
                        while ((ss = br.readLine()) != null) {
                            sb.append(ss);
                        }
                        br.close();
                        is.close();
                        Message message = handler.obtainMessage();
                        message.obj = sb;
                        handler.sendMessage(message);
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    class Myadapter extends BaseAdapter{

        private ViewHolder aa;

        public class ViewHolder{
            TextView text1;
            TextView text2;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view=getLayoutInflater().inflate(R.layout.listview_item,null);
            TextView text1= (TextView) view.findViewById(R.id.text1);
            TextView text2= (TextView) view.findViewById(R.id.text2);
            text1.setText(list.get(i).value);
            text2.setText(list.get(i).key);
            return view;
        }
    }
}
